package pcloud.org.server.resource;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.DELETE;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.PUT;

import pcloud.org.server.component.FileComponent;
import pcloud.org.server.model.FileInfo;

@At(FilesInfoResource.PATH)
@InjectName
@IocBean
public class FilesInfoResource
{
	public static final String PATH = "/filesinfo";
	private static final Log log = Logs.getLog(FilesInfoResource.class);
	@Inject
	private FileComponent fileComponent;

	@At("/*")
	@PUT
	@AdaptBy(type = JsonAdaptor.class)
	public FileInfo updateResource(String path, FileInfo targetFileInfo, HttpServletRequest request, HttpSession session) throws FileNotFoundException
	{
		String absolutePath = request.getServletPath();
		log.info("REQUEST update resource info '"+absolutePath+"'");

		FileInfo responseInfo = fileComponent.update(absolutePath, targetFileInfo, null);

		log.info("RESPONSE update resource info '"+absolutePath+"'\r\n--"+Json.toJson(responseInfo));
		return responseInfo;
	}

	@At("/*")
	@POST
	@AdaptBy(type = JsonAdaptor.class)
	public FileInfo createResource(String path, FileInfo targetFileInfo, HttpServletRequest request, HttpSession session)
	{
		String absolutePath = request.getServletPath();
		log.info("REQUEST create resource '"+absolutePath+"'");

		FileInfo responseInfo = fileComponent.create(absolutePath, targetFileInfo, null);

		log.info("RESPONSE create resource '"+absolutePath+"'\r\n--"+Json.toJson(responseInfo));
		return responseInfo;
	}

	@At("/*")
	@DELETE
	public FileInfo deleteResource(String path, HttpServletRequest request, HttpSession session) throws FileNotFoundException
	{
		String absolutePath = request.getServletPath();
		log.info("REQUEST delete resource '"+absolutePath+"'");

		FileInfo responseInfo;
		responseInfo = fileComponent.delete(absolutePath);

		log.info("RESPONSE delete resource info '"+absolutePath+"'\r\n--"+Json.toJson(responseInfo));
		return responseInfo;
	}

	@At("/*")
	@GET
	public FileInfo getResourceInfo(String path, HttpServletRequest request, HttpSession session) throws FileNotFoundException
	{
		String absolutePath = request.getServletPath();
		log.info("REQUEST get resource info '"+absolutePath+"'");
		
		FileInfo responseInfo = fileComponent.get(absolutePath);
		if (!responseInfo.exists()) {
			throw new FileNotFoundException();
		}

		log.info("RESPONSE get resource info '"+absolutePath+"'\r\n--"+Json.toJson(responseInfo));
		return responseInfo;
	}

}
